package com.yunhuisu.controller;

import com.alibaba.fastjson.JSON;
import com.yunhuisu.pojo.Books;
import com.yunhuisu.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;

@Controller
public class BookController {
    @Autowired
    BookService bookService;

    @RequestMapping(value = "/findBook", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String findBook() {
        List<Books> books = bookService.findAllBook();
        String resultBooks = JSON.toJSONString(books);
        return resultBooks;
    }
    @RequestMapping(value = "/findBook/{id}" , produces = "text/html;charset=utf-8")
    @ResponseBody
    public String findBook(@PathVariable("id") int id) {
        Books byIdBook = bookService.findByIdBook(id);
        String resultBook = JSON.toJSONString(byIdBook);
        return resultBook;
    }
    @RequestMapping(value = "/addBook",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String addBook(Books books) {
        boolean insertResult = bookService.addBook(books);
        return insertResult == true ? "插入成功" : "插入失败";
    }
    @RequestMapping(value = "/updateBook",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String updateBook(Books books) {
        boolean updateResult = bookService.addBook(books);
        return updateResult == true ? "修改成功" : "修改失败";
    }
    @RequestMapping(value = "/deleteByIdBook/{id}",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String deleteByIdBook(@PathVariable("id") int id) {
        boolean deleteResult = bookService.byIdDeleteBook(id);
        return deleteResult == true ? "删除成功" : "删除失败";
    }
}
